<template>
  <div class="login">
    <div class="left">
      <div class="top">
        <img src="@/assets/img/login-title.png" alt class="logo" />
        <span class="t1">黑马面面</span>
        <div class="line"></div>
        <span class="t2">用户登录</span>
      </div>

      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item prop="phone">
          <el-input
            v-model="form.phone"
            placeholder="请输入手机号"
            prefix-icon="el-icon-user"
            class="marginTop"
          ></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            :show-password="true"
          ></el-input>
        </el-form-item>

        <el-form-item prop="code">
          <el-row>
            <el-col :span="16">
              <el-input v-model="form.code" placeholder="请输入验证码" prefix-icon="el-icon-key"></el-input>
            </el-col>
            <el-col :span="8">
              <img :src="codeUrl" alt class="codeImg" @click="refreshCode" />
            </el-col>
          </el-row>
        </el-form-item>

        <el-form-item prop="isPass">
          <el-checkbox v-model="form.isPass">
            我已阅读并同意
            <el-link type="primary" class="agreement">用户协议</el-link>和
            <el-link type="primary" class="agreement">隐私条款</el-link>
          </el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="btn" @click="loginClick">登录</el-button>
          <br />
          <el-button type="primary" class="btn" @click="registerClick">注册</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="right">
      <img src="@/assets/img/login_banner_ele.png" alt />
    </div>

    <register ref="register"></register>
  </div>
</template>

<script>
import register from "@/views/register/register.vue";
import { login } from "@/api/login.js";
import { setToken } from "@/utils/token.js";
export default {
  components: {
    register
  },
  data() {
    return {
      form: {
        phone: "",
        password: "",
        code: "",
        isPass: ""
      },

      codeUrl: process.env.VUE_APP_URL + "/captcha?type=login",

      rules: {
        phone: [
          { required: true, message: "请填写手机号", trigger: "change" },
          {
            max: 11,
            min: 11,
            message: "手机号长度必须为11位",
            trigger: "change"
          },
          {
            validator: (rule, value, callback) => {
              let _reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
              if (_reg.test(value)) {
                callback();
              } else {
                callback("请输入正确的手机号");
              }
            },
            trigger: "change"
          }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
          { max: 12, min: 6, message: "密码长度为6-12位", trigger: "change" }
        ],
        code: [
          { required: true, message: "请输入验证码", trigger: "change" },
          { max: 4, min: 4, message: "验证码长度为4位数字", trigger: "change" }
        ],
        isPass: [
          { required: true, message: "请勾选协议", trigger: "change" },
          {
            validator: (rule, value, callback) => {
              if (value === true) {
                callback();
              } else {
                callback("请勾选协议");
              }
            },
            trigger: "change"
          }
        ]
      }
    };
  },

  methods: {
    loginClick() {
      console.log(this.isPass);
      // 表单验证
      this.$refs.form.validate(result => {
        console.log("result", result);

        if (result) {
          // 调用登录接口
          login(this.form).then(res => {
            console.log("登录接口", res.data.token);
            this.$message.success("登录成功");
            // 保存token
            setToken("token", res.data.token);
            // 跳转至home页面
            this.$router.push("/home");           
          });
        }
      });
    },

    registerClick() {
      this.$refs.register.dialogFormVisible = true;
    },

    refreshCode() {
      this.codeUrl =
        process.env.VUE_APP_URL + "/captcha?type=login&xxx=" + Date.now();
    }
  }
};
</script>

<style lang="less">
.login {
  width: 100%;
  height: 100%;
  background: linear-gradient(
    225deg,
    rgba(20, 148, 250, 1),
    rgba(2, 198, 250, 1)
  );
  display: flex;
  justify-content: space-around;
  align-items: center;

  .left {
    width: 478px;
    height: 550px;
    background-color: rgba(245, 245, 245, 1);
    padding: 43px;

    .el-input__inner {
      height: 45px;
    }

    .agreement {
      margin-top: -3px;
    }
  }

  .top {
    display: flex;
    align-items: center;

    .logo {
      width: 22px;
      height: 17px;
    }

    .t1 {
      font-size: 24px;
      color: rgba(13, 12, 12, 1);
      margin: 0 14px 0 17px;
    }

    .line {
      width: 2px;
      height: 28px;
      background-color: #3ec7da;
    }

    .t2 {
      font-size: 22px;
      color: rgba(13, 12, 12, 1);
      margin-left: 14px;
    }
  }

  .codeImg {
    width: 100%;
    height: 45px;
  }

  .marginTop {
    margin-top: 28px;
  }

  .btn {
    width: 100%;

    &:last-child {
      margin-top: 26px;
    }
  }

  .right {
    width: 633px;
    height: 435px;
  }
}
</style>